/*
 * LevelDownloadingGameStateController.hh
 *
 *  Created on: Apr 2, 2012
 *      Author: erudhes
 */

#ifndef LEVELDOWNLOADINGGAMESTATECONTROLLER_HH_
#define LEVELDOWNLOADINGGAMESTATECONTROLLER_HH_
#include <wx/wx.h>
#include <wx/file.h>
#include "IGameStateController.h"
#include "ClientStartingGameStateController.h"
#include "LogoDrawController.h"
#include "SmartPointer.h"

class NetworkEventData;
class wxGauge;

/**
 * This class handles the procedure of downloading a level from the server
 *
 * The downloading starts when the client recognizes that it does not have the level served by the server
 * And finishes when all the needed files are downloaded.
 * During this period this class handles every kind of event.
 * @see IGameStateController
 * @author Rudolf Heszele heszele@gmail.com
 * @version 2.0
 * @since 2012-05-19
 */
class LevelDownloadingGameStateController: public IGameStateController
{
	public:
		/**
		 * The only constructor takes the ClientStartingGameStateController
		 *
		 * This parameter is used to finish the configuration negotiation after the download is finished.
		 * @param clientStartingController The object handling the starting mechanism on the client side.
		 */
		LevelDownloadingGameStateController(SmartPointer<ClientStartingGameStateController> clientStartingController);
		virtual ~LevelDownloadingGameStateController();

		// Methods inherited from IController

		/**
		 * @see IController::handleMouseDown
		 */
		virtual void handleMouseDown(const wxPoint&);
		/**
		 * @see IController::handleMouseUp
		 */
		virtual void handleMouseUp(const wxPoint&);
		/**
		 * @see IController::handleMouseMotion
		 */
		virtual bool handleMouseMotion(const wxPoint&);
		/**
		 * @see IController::getBitmapToPaint
		 */
		virtual const wxBitmap& getBitmapToPaint() const;
		/**
		 * @see IController::getBitmapToDraw
		 */
		virtual wxBitmap& getBitmapToDraw();

		// Methods inherited from IGameStateController

		/**
		 * @see IGameStateContrller::handleSocketConnection
		 */
		virtual void handleSocketConnection(wxSocketEvent&);
		/**
		 * @see IGameStateContrller::handleSocketLost
		 */
		virtual void handleSocketLost(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketInput
		 */
		virtual void handleSocketInput(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketOutput
		 */
		virtual void handleSocketOutput(wxSocketEvent&);

	private:
		/**
		 * This method stores the received data in the proper file on the disk and sends back the acknowledge
		 *
		 * @see NetworkEventData
		 * @param data The object containing the received data
		 */
		void handleDownloadedData(NetworkEventData* data);
		/**
		 * This method handles the finishing of downloading of a file
		 *
		 * This method also starts to download an other file if needed, or starts the configuration negotiation
		 * - by changing the state to ClientStarting state - if the whole downloading procedure is over.
		 */
		void handleDownloadFinished();

		/**
		 * The path to the directory the files should be saved to
		 */
		wxString mDirectoryPath;
		/**
		 * The name of the file actually downloading
		 */
		wxString mFileName;
		/**
		 * The file object to write the data to the disk
		 */
		wxFile mDownloadedFile;
		/**
		 * The length of the file which is under downloading
		 */
		int mDownloadedFileLength;
		/**
		 * The number of already downloaded bytes
		 */
		int mDownloadedBytes;
		/**
		 * This is true is the downloading was rejected by the server - mainly because of the configuration on that side
		 */
		bool mIsDownloadingRejected;
		/**
		 * A progress dialog to show the downloading progress to the user
		 */
		wxDialog* mProgressDialog;
		/**
		 * wxWidgets class to show the progress bar itself
		 */
		wxGauge* mGauge;
		/**
		 * ClientStarting object used to handle the configuration negotiation after the download is finished
		 */
		SmartPointer<ClientStartingGameStateController> mClientStartingController;
		/**
		 * This object handles the drawings during this state of the application
		 *
		 * @see LogoDrawController
		 */
		LogoDrawController mLogoDrawController;
};


#endif /* LEVELDOWNLOADINGGAMESTATECONTROLLER_HH_ */
